package data;

import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import po.CoursePo;
import po.SelectCoursePo;
import po.UserPo;

public class SelectCourseDataTest {

	private static SelectCourseData scd;
	@Before
	public void setUp() throws Exception {
		try {
			scd=new SelectCourseData();
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public final void testGetCourse() {
		ArrayList<SelectCoursePo> scplist=new ArrayList<SelectCoursePo>();
		ObjectInputStream ois1;
		try {
			ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));		
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			scplist.add((SelectCoursePo)ois1.readObject());
			Assert.assertEquals(scd.getCourse(((UserPo)ois2.readObject()), "1").get(0),scplist.get(0) );
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	@Test
	public final void testGetCourse2() {
		ArrayList<SelectCoursePo> scplist=new ArrayList<SelectCoursePo>();
		ObjectInputStream ois1;
		try {
			ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));		
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			scplist.add((SelectCoursePo)ois1.readObject());
			Assert.assertEquals(scd.getCourse2(((UserPo)ois2.readObject()), "1").size(),new ArrayList<SelectCoursePo>().size());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



	}

	@Test
	public final void testSelectCourse() {

		try {
			ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));
			ObjectInputStream ois3=new ObjectInputStream(new FileInputStream("courseList1.txt"));
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			Assert.assertEquals(scd.selectCourse((UserPo)ois2.readObject(), (CoursePo)ois3.readObject(), "1"), false);


		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	@Test
	public final void testQuitCourse() {

		try {
			ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));
			ObjectInputStream ois3=new ObjectInputStream(new FileInputStream("courseList1.txt"));
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			Assert.assertEquals(scd.quitCourse((UserPo)ois2.readObject(), (CoursePo)ois3.readObject(), "1"), false);


		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public final void testGetScore() {

		try {
			ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));
			ObjectInputStream ois3=new ObjectInputStream(new FileInputStream("courseList1.txt"));
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			Assert.assertEquals(scd.getScore((UserPo)ois2.readObject(), (CoursePo)ois3.readObject(), "1"),-1);


		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public final void testGetselectcourse() {
		ArrayList<SelectCoursePo> scplist=new ArrayList<SelectCoursePo>();
		ObjectInputStream ois1;
		try {
			ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("courseList1.txt"));		
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			scplist.add((SelectCoursePo)ois1.readObject());
			Assert.assertEquals(scd.getselectcourse(((CoursePo)ois2.readObject()), "1").get(0),scplist.get(0) );
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	@Test
	public final void testUpdatecourse() {
		ArrayList<SelectCoursePo> scplist=new ArrayList<SelectCoursePo>();
		ObjectInputStream ois1;
		try {
			ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("courseList1.txt"));		
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			scplist.add((SelectCoursePo)ois1.readObject());
			Assert.assertEquals(scd.updatecourse(((CoursePo)ois2.readObject()), "1"),true );
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public final void testUpdatestudent() {

		ArrayList<SelectCoursePo> scplist=new ArrayList<SelectCoursePo>();
		ObjectInputStream ois1;
		try {
			ois1 = new ObjectInputStream(new FileInputStream("tempselectcourse1.txt"));
			ObjectInputStream ois2=new ObjectInputStream(new FileInputStream("userList.txt"));		
			ois2.readObject();
			ois2.readObject();
			ois2.readObject();
			scplist.add((SelectCoursePo)ois1.readObject());
			Assert.assertEquals(scd.updatestudent(((UserPo)ois2.readObject()), "1"),false);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}


}
